<?php 
/**
 * This file is part of DomFg.
 * 
 * DomFg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomFg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomFg.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @author Oliver Leisalu, Inditel Meedia
* @package DomFg
*/
class DomFgPost {
	
		
	const SESSION_STORED_VAR = 'DomFg_requestStoredVars';
	
	const POST_FIELDS_VAR = 'DomFg_requestVars';
	
	private $registeredVars = array();
	
	private $restoredVars = array();
	
	private static $instance;
	
	private function __construct() {

	}
	
	public static function getInstance( $restoreGroup = NULL ) {
		if( self::$instance === NULL )
			self::$instance = new DomFgPost();
			
		if( $restoreGroup !== NULL) {
			self::$instance->restoredVars = $_SESSION[self::SESSION_STORED_VAR][$restoreGroup];
		}
		return self::$instance;	
	}
	
	public static function registerInput( $input ) {
		$name = explode('[', $input->name);
		self::getInstance()->registeredVars[$name[0]] = get_class($input);
	}
	
	public function unregisterInput( DomFgInput $input ) {
		unset($this->registeredVars[$input->name]);
	}
	
	public function __get( $name ) {
		return $this->parseCell( $name );
	}
	
	public function store( $group, array $fields = array() ) {
		
		if( count($fields) == 0 ) {
			foreach( $this->getPost() as $key => $value ) {
				$_SESSION[self::SESSION_STORED_VAR][$group][$key] = $value;
			}
		} else {
			foreach( $fields as $key ) {
				$_SESSION[self::SESSION_STORED_VAR][$group][$key] = $this->__get($key);
			}
		}
		return $this;
	}
	
	public function unstore( $group, array $fields = array() ) {
		if( count($fields) == 0 ) {
			unset($_SESSION[self::SESSION_STORED_VAR]);
		} else {
			foreach( $fields as $key ) {
				unset($_SESSION[self::SESSION_STORED_VAR][$key]);
			}
		}
		return $this;
	}
	
	public function isPost( $name ) {
		return isset($_POST[$name]);
	}
	
	public function getPost() {
		$list = array();
		foreach( array_merge((array)$_FILES, (array)$_POST,(array)$_SESSION[self::SESSION_STORED_VAR]) as $key => $value )
			$list[$key] = $this->parseCell( $key );
		return $list;
	}
	
	public function getRegisteredVars( ) {
		return $this->registeredVars;
	}
	
	private function parseCell( $name ) {

		if( isset($_POST[self::POST_FIELDS_VAR][$name]) && (isset($_POST[$name]) or isset($_FILES[$name])) ) {
			$rowClassName = $_POST[self::POST_FIELDS_VAR][$name];
			class_exists($rowClassName);
			return call_user_func_array( array($rowClassName,'parsePost'), array($name, &$_POST[$name]));
		
		} else if( isset($this->restoredVars[$name]) ) {
			return $this->restoredVars[$name];
		}

		return NULL;
	}
	
	public function getFields() {
		
		$fields = '';
		foreach( $this->registeredVars as $key => $var ) {
			$fields .= '<input type="hidden" name="'.self::POST_FIELDS_VAR.'['.$key.']" value="'.$var.'" />';
		}
		return $fields;
	}
}
?>